<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sales Order Template</title>
    <style>
        * { 
            box-sizing: border-box; 
        }
        
        body { 
            font-family: 'Calibri', Arial, sans-serif; 
            font-size: 9pt; 
            color: #0c0c0c; 
            margin: 0; 
            padding: 20px; 
            background: #ffffff; 
        }
        
        .invoice-container { 
            max-width: 800px; 
            margin: 0 auto; 
            background: #ffffff; 
        }
        
        .invoice-header { 
            display: flex; 
            justify-content: space-between; 
            align-items: flex-start; 
            margin-bottom: 20px; 
        }
        
        .invoice-title { 
            font-size: 20px; 
            font-weight: 700; 
            letter-spacing: 1px; 
            color: #0c0c0c; 
            margin-bottom: 10px;
        }
        
        .invoice-meta { 
            font-size: 12px; 
            color: #666; 
        }
        
        .invoice-meta table { 
            width: 100%; 
            border-collapse: collapse; 
        }
        
        .invoice-meta td { 
            padding: 4px 8px; 
            border: none; 
        }
        
        .invoice-meta .label { 
            font-weight: 600; 
            width: 120px; 
        }
        
        .address-section { 
            display: flex; 
            justify-content: space-between; 
            margin-bottom: 20px; 
        }
        
        .address-block { 
            width: 48%; 
        }
        
        .address-title { 
            font-weight: 600; 
            font-size: 12px; 
            margin-bottom: 8px; 
            color: #0c0c0c; 
        }
        
        .address-content { 
            font-size: 11px; 
            line-height: 1.4; 
            color: #333; 
        }
        
        .items-table { 
            width: 100%; 
            border-collapse: collapse; 
            margin-top: 20px; 
        }
        
        .items-table th, 
        .items-table td { 
            border: 1px solid #d8d8d8; 
            padding: 8px; 
            text-align: left; 
            vertical-align: top; 
        }
        
        .items-table th { 
            background-color: #f1f1f1; 
            font-weight: 600; 
            font-size: 9px; 
            text-transform: uppercase; 
            letter-spacing: 0.3px; 
        }
        
        .items-table .text-right { 
            text-align: right; 
        }
        
        .item-code { 
            font-size: 9px; 
            color: #0c0c0c; 
        }
        
        .item-name { 
            font-weight: 600; 
            font-size: 9px; 
            color: #0c0c0c; 
        }
        
        .item-description { 
            font-size: 8px; 
            color: #666; 
            margin-top: 2px; 
        }
        
        .totals-table { 
            width: 100%; 
            border-collapse: collapse; 
            margin-top: 20px; 
        }
        
        .totals-table td { 
            padding: 6px 8px; 
            border: none; 
        }
        
        .totals-table .label { 
            text-align: left; 
            font-size: 10px; 
        }
        
        .totals-table .amount { 
            text-align: right; 
            font-size: 10px; 
        }
        
        .totals-table .grand-total { 
            font-weight: 700; 
            font-size: 10px; 
        }
        
        .payment-section { 
            margin-top: 30px; 
            padding: 15px; 
            background-color: #f9fafb; 
            border: 1px dashed #ccc; 
        }
        
        .payment-title { 
            font-weight: 600; 
            font-size: 9px; 
            margin-bottom: 8px; 
            color: #0c0c0c; 
        }
        
        .payment-content { 
            font-size: 9px; 
            color: #666; 
        }
        
        .company-info { 
            margin-top: 20px; 
            font-size: 11px; 
            color: #444; 
            line-height: 1.6; 
        }
        
        .company-name { 
            font-weight: 600; 
            margin-bottom: 5px; 
        }
        
        .terms-section {
            margin-top: 20px;
            padding: 15px;
            background-color: #f9fafb;
            border: 1px solid #ddd;
        }
        
        .terms-title {
            font-weight: 600;
            font-size: 12px;
            margin-bottom: 10px;
            color: #0c0c0c;
        }
        
        .terms-content {
            font-size: 10px;
            color: #333;
            line-height: 1.4;
        }
        
        .bank-details {
            margin-top: 20px;
            padding: 15px;
            background-color: #f9fafb;
            border: 1px solid #ddd;
        }
        
        .bank-title {
            font-weight: 600;
            font-size: 12px;
            margin-bottom: 10px;
            color: #0c0c0c;
        }
        
        .bank-content {
            font-size: 10px;
            color: #333;
            line-height: 1.4;
        }
        
        .bank-content table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .bank-content td {
            padding: 4px 8px;
            border: none;
        }
        
        .bank-content .label {
            font-weight: 600;
            width: 120px;
        }
        
        /* 自定义状态样式 */
        .status-custom {
            padding: 4px 8px;
            border-radius: 4px;
            font-weight: 600;
            font-size: 10px;
        }
        
        .status-to-manufacturing {
            background-color: #6f42c1;
            color: white;
        }
        
        .status-to-shipment {
            background-color: #20c997;
            color: white;
        }
        
        .status-to-deliver {
            background-color: #17a2b8;
            color: white;
        }
        
        /* 自定义字段样式 */
        .custom-fields-section {
            margin-top: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
        }
        
        .custom-fields-title {
            font-weight: 600;
            font-size: 12px;
            margin-bottom: 15px;
            color: #0c0c0c;
        }
        
        .custom-fields-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
        }
        
        .custom-field {
            display: flex;
            flex-direction: column;
        }
        
        .custom-field-label {
            font-weight: 600;
            font-size: 10px;
            margin-bottom: 5px;
            color: #333;
        }
        
        .custom-field-value {
            font-size: 10px;
            color: #666;
            padding: 5px;
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 3px;
        }
        
        .file-attachment {
            color: #007bff;
            text-decoration: underline;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="invoice-container">
        <!-- 头部信息 -->
        <div class="invoice-header">
            <div>
                <div class="invoice-title">SALES ORDER</div>
                <div class="invoice-meta">
                    <table>
                        <tr>
                            <td class="label">Order Date:</td>
                            <td>{{ doc.transaction_date if doc.transaction_date else "N/A" }}</td>
                        </tr>
                        <tr>
                            <td class="label">Order Number:</td>
                            <td>{{ doc.name if doc.name else "N/A" }}</td>
                        </tr>
                        <tr>
                            <td class="label">Reference:</td>
                            <td>{{ doc.po_no if doc.po_no else "" }}</td>
                        </tr>
                        <tr>
                            <td class="label">Delivery Date:</td>
                            <td>{{ doc.delivery_date if doc.delivery_date else "" }}</td>
                        </tr>
                        {% if doc.po_date %}
                        <tr>
                            <td class="label">PO Date:</td>
                            <td>{{ doc.po_date if doc.po_date else "" }}</td>
                        </tr>
                        {% endif %}
                    </table>
                </div>
            </div>
            <div>
                <div class="invoice-meta">
                    <table>
                        <tr>
                            <td class="label">Company:</td>
                            <td>{{ doc.company if doc.company else "N/A" }}</td>
                        </tr>
                        <tr>
                            <td class="label">Currency:</td>
                            <td>{{ doc.currency if doc.currency else "N/A" }}</td>
                        </tr>
                        <tr>
                            <td class="label">Status:</td>
                            <td>
                                {% if doc.status == "To Manufacturing" %}
                                    <span class="status-custom status-to-manufacturing">{{ doc.status }}</span>
                                {% elif doc.status == "To Shipment" %}
                                    <span class="status-custom status-to-shipment">{{ doc.status }}</span>
                                {% elif doc.status == "To Deliver" %}
                                    <span class="status-custom status-to-deliver">{{ doc.status }}</span>
                                {% else %}
                                    {{ doc.status if doc.status else "N/A" }}
                                {% endif %}
                            </td>
                        </tr>
                        {% if doc.order_type %}
                        <tr>
                            <td class="label">Order Type:</td>
                            <td>{{ doc.order_type if doc.order_type else "" }}</td>
                        </tr>
                        {% endif %}
                    </table>
                </div>
            </div>
        </div>

        <!-- 地址信息 -->
        <div class="address-section">
            <div class="address-block">
                <div class="address-title">Bill To</div>
                <div class="address-content">
                    {{ doc.customer_name if doc.customer_name else (doc.customer if doc.customer else "N/A") }}<br>
                    {{ doc.address_display if doc.address_display else "" }}<br>
                    {% if doc.tax_id %}
                        Tax ID: {{ doc.tax_id }}
                    {% endif %}
                    {% if doc.contact_display %}
                        <br>Contact: {{ doc.contact_display }}
                    {% endif %}
                </div>
            </div>
            <div class="address-block">
                <div class="address-title">Ship To</div>
                <div class="address-content">
                    {{ doc.customer_name if doc.customer_name else (doc.customer if doc.customer else "N/A") }}<br>
                    {{ doc.shipping_address if doc.shipping_address else "" }}
                    {% if doc.dispatch_address %}
                        <br><br>Dispatch Address:<br>
                        {{ doc.dispatch_address if doc.dispatch_address else "" }}
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- 自定义字段区域 - OutGoing 和 Shipping 信息 -->
        {% if doc.status in ["To Manufacturing", "To Shipment", "To Deliver"] %}
        <div class="custom-fields-section">
            <div class="custom-fields-title">
                {% if doc.status == "To Manufacturing" %}
                    生产厂家填写 (Manufacturer's Input)
                {% elif doc.status == "To Shipment" %}
                    跟单填写 (Follow-up Input)
                {% elif doc.status == "To Deliver" %}
                    物流信息 (Shipping Information)
                {% endif %}
            </div>
            
            <div class="custom-fields-grid">
                <!-- OutGoing 相关字段 -->
                {% if doc.invoice_pdf %}
                <div class="custom-field">
                    <div class="custom-field-label">Invoice PDF:</div>
                    <div class="custom-field-value file-attachment">{{ doc.invoice_pdf }}</div>
                </div>
                {% endif %}
                
                {% if doc.payment_pdf %}
                <div class="custom-field">
                    <div class="custom-field-label">Payment PDF:</div>
                    <div class="custom-field-value file-attachment">{{ doc.payment_pdf }}</div>
                </div>
                {% endif %}
                
                {% if doc.pack_pdf %}
                <div class="custom-field">
                    <div class="custom-field-label">Pack PDF:</div>
                    <div class="custom-field-value file-attachment">{{ doc.pack_pdf }}</div>
                </div>
                {% endif %}
                
                {% if doc.approve_doc %}
                <div class="custom-field">
                    <div class="custom-field-label">Approve Doc:</div>
                    <div class="custom-field-value file-attachment">{{ doc.approve_doc }}</div>
                </div>
                {% endif %}
                
                <!-- Shipping 相关字段 -->
                {% if doc.track_no %}
                <div class="custom-field">
                    <div class="custom-field-label">TrackNo:</div>
                    <div class="custom-field-value">{{ doc.track_no }}</div>
                </div>
                {% endif %}
                
                {% if doc.num_pack %}
                <div class="custom-field">
                    <div class="custom-field-label">NumPack:</div>
                    <div class="custom-field-value">{{ doc.num_pack }}</div>
                </div>
                {% endif %}
                
                {% if doc.disment %}
                <div class="custom-field">
                    <div class="custom-field-label">Disment:</div>
                    <div class="custom-field-value">{{ doc.disment }}</div>
                </div>
                {% endif %}
                
                <!-- 生产完成时间 -->
                {% if doc.production_completion_time %}
                <div class="custom-field">
                    <div class="custom-field-label">生产完成时间:</div>
                    <div class="custom-field-value">{{ doc.production_completion_time }}</div>
                </div>
                {% endif %}
                
                <!-- 报关申请 -->
                {% if doc.customs_declaration %}
                <div class="custom-field">
                    <div class="custom-field-label">报关申请:</div>
                    <div class="custom-field-value file-attachment">{{ doc.customs_declaration }}</div>
                </div>
                {% endif %}
                
                <!-- 国内收货地址 -->
                {% if doc.domestic_shipping_address %}
                <div class="custom-field">
                    <div class="custom-field-label">国内收货地址:</div>
                    <div class="custom-field-value">{{ doc.domestic_shipping_address }}</div>
                </div>
                {% endif %}
                
                <!-- 最迟发货时间 -->
                {% if doc.latest_shipment_time %}
                <div class="custom-field">
                    <div class="custom-field-label">最迟发货时间:</div>
                    <div class="custom-field-value">{{ doc.latest_shipment_time }}</div>
                </div>
                {% endif %}
            </div>
        </div>
        {% endif %}

        <!-- 项目表格 -->
        <table class="items-table">
            <thead>
                <tr>
                    <th style="width:12%;">Item Code</th>
                    <th>Item Description</th>
                    <th style="width:7%;" class="text-right">Qty</th>
                    <th style="width:10%;" class="text-right">Unit Price</th>
                    <th style="width:8%;" class="text-right">Disc %</th>
                    <th style="width:10%;" class="text-right">Discount</th>
                    <th style="width:10%;" class="text-right">Net Price</th>
                    <th style="width:10%;" class="text-right">Tax</th>
                    <th style="width:12%;" class="text-right">Amount</th>
                </tr>
            </thead>
            <tbody>
                {% if doc.items %}
                    {% for item in doc.items %}
                    <tr>
                        <td class="item-code">{{ item.item_code if item.item_code else "N/A" }}</td>
                        <td>
                            <div class="item-name">{{ item.item_name if item.item_name else "N/A" }}</div>
                            <div class="item-description">{{ item.description if item.description else "" }}</div>
                            {% if item.item_group %}
                                <div class="item-description">Group: {{ item.item_group }}</div>
                            {% endif %}
                        </td>
            <td class="text-right">{{ item.qty if item.qty else 0 }}</td>
            <td class="text-right">{{ "{:,.2f}".format(item.rate if item.rate else 0) }} {{ doc.currency if doc.currency else 'USD' }}</td>
            <td class="text-right">{{ item.discount_percentage if item.discount_percentage else 0 }}%</td>
            <td class="text-right">{{ "{:,.2f}".format((item.qty * item.rate - item.amount) if item.qty and item.rate and item.amount else 0) }} {{ doc.currency if doc.currency else 'USD' }}</td>
            <td class="text-right">{{ "{:,.2f}".format((item.amount / item.qty) if item.qty and item.amount and item.qty > 0 else 0) }} {{ doc.currency if doc.currency else 'USD' }}</td>
            <td class="text-right">{{ item.item_tax_template if item.item_tax_template else "" }}</td>
            <td class="text-right">{{ "{:,.2f}".format(item.amount if item.amount else 0) }} {{ doc.currency if doc.currency else 'USD' }}</td>
                    </tr>
                    {% endfor %}
                {% else %}
                    <tr>
                        <td colspan="9" style="text-align: center; padding: 20px; color: #666;">No items found</td>
                    </tr>
                {% endif %}
            </tbody>
        </table>

        <!-- 总计表格 -->
        <table class="totals-table">
      <tr>
        <td class="label" style="width:60%;"></td>
        <td class="label">Total Discount</td>
        <td class="amount">{{ "{:,.2f}".format(doc.discount_amount if doc.discount_amount else 0) }} {{ doc.currency if doc.currency else 'USD' }}</td>
      </tr>
      <tr>
        <td></td>
        <td class="label">Subtotal</td>
        <td class="amount">{{ "{:,.2f}".format(doc.net_total if doc.net_total else 0) }} {{ doc.currency if doc.currency else 'USD' }}</td>
      </tr>
      {% if doc.taxes %}
        {% for tax in doc.taxes %}
          {% if tax.tax_amount and tax.tax_amount != 0 %}
          <tr>
            <td></td>
            <td class="label">Total {{ tax.description if tax.description else (tax.account_head if tax.account_head else "Tax") }}</td>
            <td class="amount">{{ "{:,.2f}".format(tax.tax_amount) }} {{ doc.currency if doc.currency else 'USD' }}</td>
          </tr>
          {% endif %}
        {% endfor %}
      {% endif %}
      <tr class="grand-total">
        <td></td>
        <td class="label">Order Total ({{ doc.currency if doc.currency else "USD" }})</td>
        <td class="amount">{{ "{:,.2f}".format(doc.grand_total if doc.grand_total else 0) }} {{ doc.currency if doc.currency else 'USD' }}</td>
      </tr>
        </table>

        <!-- 支付信息 -->
        <div class="payment-section">
            <div class="payment-title">Payment Information</div>
            <div class="payment-content">
                Please contact us for payment details and terms.
                {% if doc.payment_terms_template %}
                    <br><br>Payment Terms: {{ doc.payment_terms_template }}
                {% endif %}
            </div>
        </div>

        <!-- 银行信息 -->
        {% set company_default_account = frappe.db.get_value("Company", doc.company, "default_bank_account") if doc.company else None %}
        {% if company_default_account %}
            {% set bank_account_name = frappe.db.get_value("Bank Account", {"account": company_default_account, "company": doc.company, "is_company_account": 1}, "name") %}
            {% if bank_account_name and frappe.db.exists("Bank Account", bank_account_name) %}
                {% set bank_account_doc = frappe.get_doc("Bank Account", bank_account_name) %}
                {% set bank_doc = frappe.get_doc("Bank", bank_account_doc.bank) if bank_account_doc.bank and frappe.db.exists("Bank", bank_account_doc.bank) else None %}
            <div class="bank-details">
                <div class="bank-title">Pay By EFT - Bank Details</div>
                <div class="bank-content">
                    <table>
                        {% if bank_doc and bank_doc.bank_name %}
                        <tr>
                            <td class="label">Bank:</td>
                            <td>{{ bank_doc.bank_name }}{% if bank_doc.address_html %}, {{ bank_doc.address_html | striptags }}{% endif %}{% if bank_doc.swift_number %}, BIC/SWIFT: {{ bank_doc.swift_number }}{% endif %}</td>
                        </tr>
                        {% endif %}
                        {% if bank_account_doc.account_name %}
                        <tr>
                            <td class="label">Account name:</td>
                            <td>{{ bank_account_doc.account_name }}</td>
                        </tr>
                        {% endif %}
                        {% if bank_account_doc.iban %}
                        <tr>
                            <td class="label">IBAN:</td>
                            <td>{{ bank_account_doc.iban }}</td>
                        </tr>
                        {% endif %}
                        {% if bank_account_doc.bank_account_no %}
                        <tr>
                            <td class="label">Account Number:</td>
                            <td>{{ bank_account_doc.bank_account_no }}{% if bank_account_doc.account_currency %} ({{ bank_account_doc.account_currency }} Only){% endif %}</td>
                        </tr>
                        {% endif %}
                        {% if bank_account_doc.branch_code %}
                        <tr>
                            <td class="label">Branch Code:</td>
                            <td>{{ bank_account_doc.branch_code }}</td>
                        </tr>
                        {% endif %}
                        {% if bank_account_doc.address_html %}
                        <tr>
                            <td class="label">Bank Address:</td>
                            <td>{{ bank_account_doc.address_html | safe }}</td>
                        </tr>
                        {% endif %}
                    </table>
                </div>
            </div>
            {% else %}
            <!-- 银行账户不存在时显示默认信息 -->
            <div class="bank-details">
                <div class="bank-title">Pay By EFT - Bank Details</div>
                <div class="bank-content">
                    <table>
                        <tr>
                            <td class="label">Note:</td>
                            <td>Bank account not found. Please contact us for payment details.</td>
                        </tr>
                    </table>
                </div>
            </div>
            {% endif %}
        {% else %}
        <!-- 默认银行信息（当系统没有配置银行信息时显示） -->
        <div class="bank-details">
            <div class="bank-title">Pay By EFT - Bank Details</div>
            <div class="bank-content">
                <table>
                    <tr>
                        <td class="label">Note:</td>
                        <td>Please contact us for bank details and payment information.</td>
                    </tr>
                </table>
            </div>
        </div>
        {% endif %}

        <!-- 条款和条件 -->
        {% if doc.terms %}
        <div class="terms-section">
            <div class="terms-title">Terms and Conditions</div>
            <div class="terms-content">{{ doc.terms if doc.terms else "" }}</div>
        </div>
        {% endif %}

        <!-- 公司信息 -->
        <div class="company-info">
            <div class="company-name">{{ doc.company if doc.company else "Company Name" }}</div>
            {{ doc.company_address_display if doc.company_address_display else "Company Address" }}<br>
            {% if doc.company_contact_person %}
                Contact: {{ doc.company_contact_person }}<br>
            {% endif %}
            {% if doc.contact_phone %}
                Phone: {{ doc.contact_phone }}<br>
            {% endif %}
            {% if doc.contact_email %}
                Email: {{ doc.contact_email }}<br>
            {% endif %}
        </div>
    </div>

  <script>
    // 货币符号映射
    const currencySymbols = {
      'USD': '$', 'EUR': '€', 'GBP': '£', 'JPY': '¥', 'CNY': '¥',
      'AUD': 'A$', 'CAD': 'C$', 'CHF': 'CHF', 'HKD': 'HK$', 'SGD': 'S$',
      'KRW': '₩', 'INR': '₹', 'BRL': 'R$', 'MXN': 'MX$', 'RUB': '₽',
      'ZAR': 'R', 'NZD': 'NZ$', 'SEK': 'kr', 'NOK': 'kr', 'DKK': 'kr',
      'PLN': 'zł', 'CZK': 'Kč', 'HUF': 'Ft', 'RON': 'lei', 'BGN': 'лв',
      'HRK': 'kn', 'RSD': 'дин', 'UAH': '₴', 'TRY': '₺', 'ILS': '₪',
      'AED': 'د.إ', 'SAR': 'ر.س', 'QAR': 'ر.ق', 'KWD': 'د.ك', 'BHD': 'د.ب',
      'OMR': 'ر.ع.', 'JOD': 'د.ا', 'LBP': 'ل.ل', 'EGP': '£', 'MAD': 'د.م.',
      'TND': 'د.ت', 'DZD': 'د.ج', 'LYD': 'ل.د', 'SDG': 'ج.س.', 'ETB': 'Br',
      'KES': 'KSh', 'UGX': 'USh', 'TZS': 'TSh', 'ZMW': 'ZK', 'BWP': 'P',
      'SZL': 'L', 'LSL': 'L', 'NAD': 'N$', 'MZN': 'MT', 'AOA': 'Kz',
      'XOF': 'CFA', 'XAF': 'FCFA', 'CDF': 'FC', 'RWF': 'RF', 'BIF': 'FBu',
      'DJF': 'Fdj', 'KMF': 'CF', 'MGA': 'Ar', 'MUR': '₨', 'SCR': '₨',
      'SLL': 'Le', 'GMD': 'D', 'GHS': '₵', 'NGN': '₦', 'XPF': '₣',
      'TOP': 'T$', 'WST': 'WS$', 'VUV': 'Vt', 'FJD': 'FJ$', 'PGK': 'K',
      'SBD': 'SI$', 'VND': '₫', 'THB': '฿', 'MYR': 'RM', 'IDR': 'Rp',
      'PHP': '₱', 'MMK': 'K', 'LAK': '₭', 'KHR': '៛', 'BDT': '৳',
      'PKR': '₨', 'LKR': '₨', 'NPR': '₨', 'AFN': '؋', 'TJS': 'SM',
      'TMT': 'T', 'UZS': 'лв', 'KZT': '₸', 'KGS': 'лв', 'TMM': 'T',
      'MNT': '₮', 'AMD': '֏', 'GEL': '₾', 'AZN': '₼', 'MDL': 'L',
      'BYN': 'Br', 'LTL': 'Lt', 'LVL': 'Ls', 'EEK': 'kr', 'ISK': 'kr',
      'MKD': 'ден', 'ALL': 'L', 'BAM': 'КМ', 'CSD': 'дин', 'RSD': 'дин',
      'UYU': '$U', 'ARS': '$', 'CLP': '$', 'COP': '$', 'PEN': 'S/',
      'BOB': 'Bs', 'VES': 'Bs.S', 'PYG': '₲', 'GYD': 'G$', 'SRD': '$',
      'TTD': 'TT$', 'BBD': 'Bds$', 'JMD': 'J$', 'BZD': 'BZ$', 'GTQ': 'Q',
      'HNL': 'L', 'NIO': 'C$', 'CRC': '₡', 'PAB': 'B/.', 'DOP': 'RD$',
      'HTG': 'G', 'CUP': '$', 'CUC': '$', 'XCD': 'EC$', 'AWG': 'ƒ',
      'ANG': 'ƒ', 'BMD': 'BD$', 'KYD': 'CI$'
    };

    // 格式化货币显示（用于模板）
    function format_currency(amount, currency = 'USD') {
      if (!amount || isNaN(amount)) return '0.00';
      
      const symbol = currencySymbols[currency] || currency;
      const formattedAmount = parseFloat(amount).toFixed(2);
      
      // 根据货币类型决定符号位置
      if (['USD', 'CAD', 'AUD', 'NZD', 'HKD', 'SGD', 'BBD', 'BZD', 'JMD', 'TTD', 'XCD', 'BMD', 'KYD',
          'BHD', 'QAR', 'KWD', 'OMR', 'JOD', 'LBP', 'EGP', 'MAD', 'TND', 'DZD', 'LYD', 'SDG', 'ETB',
          'KES', 'UGX', 'TZS', 'ZMW', 'BWP', 'SZL', 'LSL', 'NAD', 'MZN', 'AOA', 'XOF', 'XAF', 'CDF',
          'RWF', 'BIF', 'DJF', 'KMF', 'MGA', 'MUR', 'SCR', 'SLL', 'GMD', 'GHS', 'NGN', 'XPF', 'TOP',
          'WST', 'VUV', 'FJD', 'PGK', 'SBD', 'VND', 'THB', 'MYR', 'IDR', 'PHP', 'MMK', 'LAK', 'KHR',
          'BDT', 'PKR', 'LKR', 'NPR', 'AFN', 'TJS', 'TMT', 'UZS', 'KZT', 'KGS', 'TMM', 'MNT', 'AMD',
          'GEL', 'AZN', 'MDL', 'BYN', 'LTL', 'LVL', 'EEK', 'ISK', 'MKD', 'ALL', 'BAM', 'CSD', 'RSD',
          'UYU', 'ARS', 'CLP', 'COP', 'PEN', 'BOB', 'VES', 'PYG', 'GYD', 'SRD', 'TTD', 'BBD', 'JMD',
          'BZD', 'GTQ', 'HNL', 'NIO', 'CRC', 'PAB', 'DOP', 'HTG', 'CUP', 'CUC', 'XCD', 'AWG', 'ANG',
          'BMD', 'KYD'].includes(currency)) {
        return symbol + formattedAmount;
      } else {
        return formattedAmount + ' ' + symbol;
      }
    }

    // 页面加载完成后初始化
    document.addEventListener('DOMContentLoaded', function() {
      console.log('Sales Order Template with Currency Support loaded');
    });
  </script>
</body>
</html>
